package com.ruoyi.mall.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mall.domain.MallMemberUser;
import com.ruoyi.mall.domain.MallTimeWarehouse;
import com.ruoyi.mall.mapper.MallMemberUserMapper;
import com.ruoyi.mall.mapper.MallTimeWarehouseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.mapper.MallTimeGoodsMapper;
import com.ruoyi.mall.domain.MallTimeGoods;
import com.ruoyi.mall.service.IMallTimeGoodsService;

/**
 * 抢购商品Service业务层处理
 *
 * @author ruoyi
 * @date 2022-02-25
 */
@Service
public class MallTimeGoodsServiceImpl implements IMallTimeGoodsService
{
    @Autowired
    private MallTimeGoodsMapper mallTimeGoodsMapper;
    @Autowired
    private MallMemberUserMapper mallMemberUserMapper;
    @Autowired
    private MallTimeWarehouseMapper mallTimeWarehouseMapper;

    /**
     * 查询抢购商品
     *
     * @param id 抢购商品主键
     * @return 抢购商品
     */
    @Override
    public MallTimeGoods selectMallTimeGoodsById(Long id)
    {
        return mallTimeGoodsMapper.selectMallTimeGoodsById(id);
    }

    /**
     * 查询抢购商品列表
     *
     * @param mallTimeGoods 抢购商品
     * @return 抢购商品
     */
    @Override
    public List<MallTimeGoods> selectMallTimeGoodsList(MallTimeGoods mallTimeGoods)
    {
        return mallTimeGoodsMapper.selectMallTimeGoodsList(mallTimeGoods);
    }

    /**
     * 新增抢购商品
     *
     * @param mallTimeGoods 抢购商品
     * @return 结果
     */
    @Override
    public int insertMallTimeGoods(MallTimeGoods mallTimeGoods)
    {
        MallMemberUser mallMemberUser = mallMemberUserMapper.selectMallMemberUserById(mallTimeGoods.getUserId());
        if (mallMemberUser != null) {
            mallTimeGoods.setUserName(mallMemberUser.getName()+"--"+mallMemberUser.getPhone());
        }
        MallTimeWarehouse mallTimeWarehouse = mallTimeWarehouseMapper.selectMallTimeWarehouseById(mallTimeGoods.getRegionId());
        if (mallTimeWarehouse != null) {
            mallTimeGoods.setRegionName(mallTimeWarehouse.getTitle());
        }
        mallTimeGoods.setCreateTime(DateUtils.getNowDate());
        return mallTimeGoodsMapper.insertMallTimeGoods(mallTimeGoods);
    }

    /**
     * 修改抢购商品
     *
     * @param mallTimeGoods 抢购商品
     * @return 结果
     */
    @Override
    public int updateMallTimeGoods(MallTimeGoods mallTimeGoods)
    {
        MallMemberUser mallMemberUser = mallMemberUserMapper.selectMallMemberUserById(mallTimeGoods.getUserId());
        if (mallMemberUser != null) {
            mallTimeGoods.setUserName(mallMemberUser.getName()+"--"+mallMemberUser.getPhone());
        }
        MallTimeWarehouse mallTimeWarehouse = mallTimeWarehouseMapper.selectMallTimeWarehouseById(mallTimeGoods.getRegionId());
        if (mallTimeWarehouse != null) {
            mallTimeGoods.setRegionName(mallTimeWarehouse.getTitle());
        }
        mallTimeGoods.setUpdateTime(DateUtils.getNowDate());
        return mallTimeGoodsMapper.updateMallTimeGoods(mallTimeGoods);
    }

    /**
     * 批量删除抢购商品
     *
     * @param ids 需要删除的抢购商品主键
     * @return 结果
     */
    @Override
    public int deleteMallTimeGoodsByIds(Long[] ids)
    {
        return mallTimeGoodsMapper.deleteMallTimeGoodsByIds(ids);
    }

    /**
     * 删除抢购商品信息
     *
     * @param id 抢购商品主键
     * @return 结果
     */
    @Override
    public int deleteMallTimeGoodsById(Long id)
    {
        return mallTimeGoodsMapper.deleteMallTimeGoodsById(id);
    }

    /**
     * 查询抢购商品列表（isPublish 是否发布：0-未发布 1-已发布 2-正在交易（已售罄））
     *
     * @return 抢购商品集合
     */
    @Override
    public List<MallTimeGoods> selectIsPublishTimeGoodsList(MallTimeGoods mallTimeGoods) {
        return mallTimeGoodsMapper.selectIsPublishTimeGoodsList(mallTimeGoods);
    }
}
